package com.example.learnx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.learnx.mapper.SysDepartMapper;
import com.example.learnx.model.dto.SuperiorRequest;
import com.example.learnx.model.dto.UpdateStudentOrgRequest;
import com.example.learnx.model.dto.UpdateStudentRequest;
import com.example.learnx.model.entity.SysDepart;
import com.example.learnx.model.entity.SysUser;
import com.example.learnx.service.SysUserService;
import com.example.learnx.mapper.SysUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author L
* @description 针对表【sys_user(用户表)】的数据库操作Service实现
* @createDate 2023-09-08 14:52:31
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{

    @Resource
    SysDepartMapper sysDepartMapper;
    @Resource
    SysUserMapper sysUserMapper;
    @Override
    public Boolean setOrg(UpdateStudentOrgRequest updateStudentOrgRequest) {
        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        sysUserQueryWrapper.eq("username",updateStudentOrgRequest.getUsername()).eq("realname",updateStudentOrgRequest.getRealname());

        SysUser one = getOne(sysUserQueryWrapper);

        if(one==null){
            System.out.println("用户名与账号对不上！");
            return Boolean.FALSE;
        }
        QueryWrapper<SysDepart> sysDepartQueryWrapper = new QueryWrapper<>();

        sysDepartQueryWrapper.eq("depart_name",updateStudentOrgRequest.getOrgCodeTxt());

        SysDepart sysDepart = sysDepartMapper.selectOne(sysDepartQueryWrapper);

        if (sysDepart==null){

            System.out.println("部门错误!");

            return Boolean.FALSE;
        }

        one.setOrgCode(sysDepart.getOrgCode());
        one.setOrgCodeTxt(updateStudentOrgRequest.getOrgCodeTxt());

        updateById(one);

        return Boolean.TRUE;
    }

    @Override
    public Boolean setSuperior(SuperiorRequest superiorRequest) {

        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        sysUserQueryWrapper.eq("username",superiorRequest.getUsername()).eq("realname",superiorRequest.getRealname());

        SysUser one = getOne(sysUserQueryWrapper);

        if(one==null){
            System.out.println("用户名与账号对不上！");
            return Boolean.FALSE;
        }

        QueryWrapper<SysDepart> sysDepartQueryWrapper = new QueryWrapper<>();

        sysDepartQueryWrapper.eq("org_code",one.getOrgCode());

        SysDepart sysDepart = sysDepartMapper.selectOne(sysDepartQueryWrapper);

        if (sysDepart==null){

            System.out.println("部门错误!");

            return Boolean.FALSE;
        }

        one.setUserIdentity(2);
        one.setDepartIds(sysDepart.getId());

        updateById(one);

        return Boolean.TRUE;
    }

    @Override
    public Boolean updateInfo(UpdateStudentRequest updateStudentRequest) {
        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        sysUserQueryWrapper.eq("username",updateStudentRequest.getUsername()).eq("realname",updateStudentRequest.getRealname());

        SysUser one = getOne(sysUserQueryWrapper);
        if(one==null){
            System.out.println("用户名与账号对不上！");
            return Boolean.FALSE;
        }

        one.setGrade(updateStudentRequest.getGrade());
        one.setClazz(updateStudentRequest.getClazz());
        one.setOrgCodeTxt(updateStudentRequest.getOrgCodeTxt());

        QueryWrapper<SysDepart> sysDepartQueryWrapper = new QueryWrapper<>();

        sysDepartQueryWrapper.eq("depart_name",one.getOrgCodeTxt());

        SysDepart sysDepart = sysDepartMapper.selectOne(sysDepartQueryWrapper);

        if (sysDepart==null){

            System.out.println("部门错误!");

            return Boolean.FALSE;
        }

        one.setOrgCode(sysDepart.getOrgCode());
        one.setDelFlag(0);
        one.setStatus(1);

        updateById(one);

        return Boolean.TRUE;
    }

    @Override
    public String setStatus(String studentAccount) {
        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        sysUserQueryWrapper.eq("username",studentAccount);

        SysUser one = getOne(sysUserQueryWrapper);
        if(one==null){

            return "账号是否正确！";
        }

        one.setStatus(1);
        one.setDelFlag(0);

        updateById(one);

        return "可以正常登陆了！";
    }

    @Override
    public Boolean refund() {

//        QueryWrapper<SysUser> eq = new QueryWrapper<SysUser>().eq("grade", 2023).eq("org_code_txt", "体育学院").eq("user_identity", 1);
//
//        List<SysUser> sysUsers = sysUserMapper.selectList(eq);
//
//        System.out.println(sysUsers.size());
//
//        for (SysUser sysUser : sysUsers) {
//            sysUser.setDelFlag(0);
//
//            sysUser.setOrgCode("A12A15");
//            sysUserMapper.updateById(sysUser);
//        }
        return Boolean.TRUE;
    }
}




